package cn.xfdzx.android.apps.shop.activity;


import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tbruyelle.rxpermissions2.RxPermissions;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.OrderListBeen;
import cn.xfdzx.android.apps.shop.util.GlideUtils;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.util.UtilsData;
import io.reactivex.functions.Consumer;

public class WelcomeActivity extends BaseMvpActivity {

    /**
     * 权限组
     */
    private static final String[] permissionsGroup = new String[]{Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};


    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initView() {
        //请求权限

        dialog = new Dialog(this, R.style.ExitDialog);
        View layout = View.inflate(this, R.layout.dialog_agreement, null);
        layout.findViewById(R.id.tvw_dialog_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                finish();
            }
        });
        layout.findViewById(R.id.tvw_dialog_determine).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UtilsData.getPreference(WelcomeActivity.this, "welcome").put("isAgree", true);
                dialog.dismiss();
                requestPermissions();
            }
        });
        layout.findViewById(R.id.tv_user_agreement).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentuser = new Intent(WelcomeActivity.this, UserAgreementActivity.class);
                intentuser.putExtra("type", "0");
                startActivity(intentuser);
            }
        });
        layout.findViewById(R.id.tv_privacy_agreement).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentprivacy = new Intent(WelcomeActivity.this, UserAgreementActivity.class);
                intentprivacy.putExtra("type", "1");
                startActivity(intentprivacy);
            }
        });
        dialog.setContentView(layout);
        dialog.setCanceledOnTouchOutside(true);

        if (UtilsData.getPreference(this, "welcome").getBoolean("isAgree")) {
            requestPermissions();
        } else {
            dialog.show();
        }
    }

    @Override
    public void showLoading() {

    }

    /**
     * 动态权限申请
     */
    public void requestPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(permissionsGroup).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {
                if (aBoolean) {

                    runnable.run();
                } else {
                    requestPermissions();
                }
            }
        });
    }

    int countdownTime = 3;

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {

            countdownTime -= 1;//倒计时总时间减1
            if (countdownTime == 0) {
                //申请的权限全部允许
                Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
            handler.postDelayed(this, 1000);

        }
    };

    @Override
    protected void onDestroy() {
        handler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }
}
